
package kaizone.haining.chaobohui.widget;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.args.AppArgs;
import kaizone.haining.chaobohui.json.Banner;
import kaizone.haining.chaobohui.json.BannerDO;
import kaizone.haining.chaobohui.json.Banner.PicModel;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import kaizone.haining.chaobohui.widget.adv.CircleFlowIndicator;
import kaizone.haining.chaobohui.widget.adv.ViewFlow;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class FlowPanelView extends FrameLayout {

    private List<Object> mData = new ArrayList<Object>();

    private ImageFetcher mImageFetcher;

    private AdsAdapter mAdapter;

    private ViewFlow mViewFlow;

    private TextView mTextView;

    public FlowPanelView(Context context) {
        super(context);
        init();
    }

    public FlowPanelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initBannerDO();
    }

    private void initBannerDO() {
        BannerDO bannerDO = AppArgs.sBannerDO;
        if (bannerDO == null)
            return;
        Banner banner = bannerDO.getBanner(Banner.INDEX);
        if (banner != null) {
            List<PicModel> items = banner.getPicModels();
            mData.removeAll(mData);
            mData.addAll(items);
            return;
        }
    }

    public void setImageFetcher(ImageFetcher imageFetcher) {
        mImageFetcher = imageFetcher;
        if (mAdapter != null) {
            if (mData != null || !mData.isEmpty()) {
                final int count = mData.size();
                mViewFlow.setmSideBuffer(count);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    public void update(List<PicModel> data) {
        if (data == null || data.isEmpty())
            return;
        mData.removeAll(mData);
        mData.addAll(data);
        mAdapter.notifyDataSetChanged();
        final int count = mData.size();
        mViewFlow.setmSideBuffer(count);
    }

    @Override
    protected void onFinishInflate() {
        mViewFlow = (ViewFlow) findViewById(R.id.vf_ad);
        mAdapter = new AdsAdapter();
        mViewFlow.setAdapter(mAdapter);
        CircleFlowIndicator indicator = (CircleFlowIndicator) findViewById(R.id.vf_indic_ad);
        mViewFlow.setFlowIndicator(indicator);
        mViewFlow.setOnViewSwitchListener(new ViewFlow.ViewSwitchListener() {
            @Override
            public void onSwitched(View view, int position) {
                updateBanner(position);
            }
        });
        mViewFlow.setOnItemClickListener(new ViewFlow.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                openAd(position);
            }
        });
        // mViewFlow.setLayoutParams(new
        // FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 150));
        mTextView = (TextView) findViewById(R.id.tv01);
        updateBanner(0);
    }

    public void setViewFlowHeight(int height) {
        mViewFlow.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
    }

    private void updateBanner(int args) {
        if (mTextView == null)
            return;
        if (mData == null && mData.isEmpty())
            return;
        if (args >= mData.size())
            return;
        PicModel picModel = (PicModel) mData.get(args);
        mTextView.setText(picModel.name);
    }

    private void openAd(int arg0) {
        if (mData == null && mData.isEmpty())
            return;
        PicModel picModel = (PicModel) mData.get(arg0);
        if (picModel == null)
            return;
        final String urlString = picModel.clickUrl;
        Utils.openBrowser(getContext(), urlString);
    }

    private class AdsAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData == null || mData.isEmpty() ? 0 : mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData == null || mData.isEmpty() ? null : mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LinearLayout.LayoutParams layoutFill = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.FILL_PARENT);
                ImageView view = new ImageView(getContext());
                view.setScaleType(ScaleType.CENTER_CROP);
                view.setId(1);
                LinearLayout layout = new LinearLayout(getContext());
                layout.addView(view, layoutFill);
                convertView = layout;
            }
            ImageView view = (ImageView) convertView.findViewById(1);
            if (mImageFetcher != null) {
                PicModel item = (PicModel) mData.get(position);
                if (item != null) {
                    String url = item.picUrl;
                    mImageFetcher.loadImage(url, view);
                }
            }
            return convertView;
        }
    }

}
